package com.dwxt.parabolic.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.DateUtils;
import com.dwxt.parabolic.base.Graph;
import com.dwxt.parabolic.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.util.Calendar;
import java.util.Date;

@Service
public class GraphService {
    private static final SimpleDateFormat MONTH = new SimpleDateFormat("yyyy-MM");
    private static final SimpleDateFormat DAY = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat HOUR = new SimpleDateFormat("yyyy-MM-dd HH");
    @Autowired
    private EventService eventService;

    public BaseResult lookup(String companyId, Integer type) throws Exception {
        Graph graph = Graph.builder().build();
        Date[] dates = generateStartAndEndTime(type);
        Date date = dates[0];
        String currentCompanyId = RequestUtil.getCurrentCompanyId();
        while (!dates[1].before(date)) {
            graph.getxArr().add(formatDate(date, type));
            int count = eventService.countByCompanyIdAndTime(companyId, date.getTime() / 1000, type, currentCompanyId);
            graph.getyArr().add(count);
            //时间加1
            date = datePlus(date, type);
        }
        return BaseResult.ok(graph);
    }

    private String formatDate(Date date, Integer type) throws Exception {
        if (type == 1) {
            return MONTH.format(date);
        }
        if (type == 2) {
            return DAY.format(date);
        }
        if (type == 3) {
            return HOUR.format(date);
        }
        throw new Exception("类型错误");
    }

    private Date datePlus(Date date, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (type == 1) {
            calendar.add(Calendar.MONTH, 1);
        } else if (type == 2) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        } else if (type == 3) {
            calendar.add(Calendar.HOUR_OF_DAY, 1);
        }
        //date = calendar.getTime();
        return calendar.getTime();
    }

    private Date[] generateStartAndEndTime(Integer type) throws Exception {
        Date[] dates;
        switch (type) {
            //年
            case 1:
                dates = generateYear();
                break;
            //月
            case 2:
                dates = generateMonth();
                break;
            //日
            case 3:
                dates = generateDay();
                break;
            default:
                throw new Exception("类型错误");
        }
        return dates;
    }

    private Date[] generateDay() throws ParseException {

        Calendar now = Calendar.getInstance();
        now.add(Calendar.HOUR_OF_DAY, -1);
        String end = HOUR.format(now.getTime());
        now.add(Calendar.HOUR_OF_DAY, -23);
        String start = HOUR.format(now.getTime());

        Date startTime = HOUR.parse(start);
        Date endTime = HOUR.parse(end);
        return new Date[]{startTime, endTime};
    }

    private Date[] generateMonth() throws ParseException {

        Calendar now = Calendar.getInstance();
        String end = DAY.format(now.getTime());
        now.add(Calendar.DAY_OF_MONTH, -30);
        String start = DAY.format(now.getTime());

        Date startTime = DAY.parse(start);
        Date endTime = DAY.parse(end);
        return new Date[]{startTime, endTime};
    }

    private Date[] generateYear() throws ParseException {
        Calendar now = Calendar.getInstance();
        String end = MONTH.format(now.getTime());
        now.add(Calendar.MONTH, -11);
        String start = MONTH.format(now.getTime());

        Date startTime = MONTH.parse(start);
        Date endTime = MONTH.parse(end);
        return new Date[]{startTime, endTime};
    }
}
